<script setup lang="ts">
import { ref } from 'vue';
// import { useRouter } from 'vue-router';

import { message } from 'ant-design-vue';

// import { use1688todyStroe } from '#/store';
// import { in1688ToDoudianStore } from '#/store/modules';

const getUrlData = ref('');
const bindOffId: any = ref([]);
const linkLength = ref(0);

// const in1688ToDoudian: any = in1688ToDoudianStore();
// const use1688tody = use1688todyStroe();
// const router = useRouter();
function moterUrls() {
  // cheackUrlLink();
  bindOffId.value = [];

  // 使用回车 空格 分割
  const urls = getUrlData.value.split(/\s+/).filter(Boolean);
  // 最多10条链接
  cheackUrlLink(urls);
  // 检查链接是否正确
}

function cheackUrlLink(urls: string[]) {
  try {
    const linkUrls: string[] = [];
    urls.forEach((url) => {
      const urlObj = new URL(url);
      // console.log(urlObj);
      const in1688links = urlObj.origin + urlObj.pathname;
      const pattern = /offer\/(\d+).html/;
      if (!pattern.test(in1688links)) {
        throw new Error('请输入正确的1688货源链接');
      }
      const match = in1688links.match(pattern);
      if (match) {
        if (!linkUrls.includes(in1688links)) {
          // 提取并打印数字
          linkUrls.push(in1688links);
          const number = match[1];
          bindOffId.value.push(number);
          // console.log('提取的数字是:', number);
        }
      } else {
        throw new Error('请输入正确的链接');
      }
      getUrlData.value = linkUrls.join('\n');
      linkLength.value = linkUrls.length;
      if (linkUrls.length > 10) {
        throw new Error('最多支持10条链接');
      }
    });
  } catch (error: any) {
    message.error(error.message);
  }
}
const showAddBox = ref(false);

function puterConter() {
  if (linkLength.value > 10) {
    return message.error('最多支持10条链接');
  }
  showAddBox.value = true;
  // use1688tody.openFn({
  //   selectGoods: bindOffId.value,
  // });
  // use1688tody.openStatus = true;
  // in1688ToDoudian.setSelectData({
  //   selectGoods: bindOffId.value,
  // });
}
// function gotoOuer() {
//   //    window.location.href = 'https://doulingyang.cn?isinit=1';
//   router.replace({
//     name: '1688StrictSelection',
//   });
// }
</script>

<template>
  <div class="p-4">
    <a-space class="w-full" direction="vertical">
      <div>支持1688货源链接，单词最多支持10条链接，使用回车隔开每个链接</div>
      <a-space :size="2" class="w-full" direction="vertical">
        <a-textarea
          v-model:value="getUrlData"
          :rows="10"
          placeholder="请输入1688链接，单次最多支持10个链接"
          style="width: 50%"
          @blur="moterUrls"
        />
        <div class="text-right" style="width: 50%">
          <span :class="{ 'text-red-500': linkLength > 10 }">{{
            linkLength
          }}</span>
          /
          <span>10</span>
        </div>
      </a-space>

      <a-button type="primary" @click="puterConter">立即铺货</a-button>
      <!-- <a-button type="primary" @click="gotoOuer">立即铺货</a-button> -->
    </a-space>
    <set-add-product-box
      v-if="showAddBox"
      v-model="showAddBox"
      :select-item-list="bindOffId"
    />
  </div>
</template>
